<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 带View的播放器123456789101112- (IBAction)moviePlayerViewController:(id)sender &amp;#123;    // 带View的播放器的控制器        //1. 获取URL地址    NSURL *url = [[NSBundle mainBundle] URLForResource:@&amp;quot;Cupid_高清.mp4">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS-Video(视频)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/iOS-Special-Function/iOS-Video(视频)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 带View的播放器123456789101112- (IBAction)moviePlayerViewController:(id)sender &amp;#123;    // 带View的播放器的控制器        //1. 获取URL地址    NSURL *url = [[NSBundle mainBundle] URLForResource:@&amp;quot;Cupid_高清.mp4">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T10:56:01.456Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS-Video(视频)">
<meta name="twitter:description" content="####1. 带View的播放器123456789101112- (IBAction)moviePlayerViewController:(id)sender &amp;#123;    // 带View的播放器的控制器        //1. 获取URL地址    NSURL *url = [[NSBundle mainBundle] URLForResource:@&amp;quot;Cupid_高清.mp4">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/iOS-Special-Function/iOS-Video(视频)/"/>





  <title>iOS-Video(视频) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/iOS-Special-Function/iOS-Video(视频)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">iOS-Video(视频)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 带View的播放器<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">- (IBAction)moviePlayerViewController:(id)sender &#123;</span><br><span class="line">    // 带View的播放器的控制器</span><br><span class="line">    </span><br><span class="line">    //1. 获取URL地址</span><br><span class="line">    NSURL *url = [[NSBundle mainBundle] URLForResource:@&quot;Cupid_高清.mp4&quot; withExtension:nil];</span><br><span class="line">    </span><br><span class="line">    //2. 创建带View的播放器</span><br><span class="line">    MPMoviePlayerViewController *mpVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];</span><br><span class="line">    </span><br><span class="line">    //3. 模态视图弹出 --&gt; 模态视图的切换应该在View完全展示之后进行</span><br><span class="line">    [self presentViewController:mpVC animated:YES completion:nil];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<a id="more"></a>
<p>####2. 不带View的播放器<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;MediaPlayer/MediaPlayer.h&gt;</span><br><span class="line">#import &lt;AVKit/AVKit.h&gt;</span><br><span class="line"></span><br><span class="line">@interface ViewController ()</span><br><span class="line"></span><br><span class="line">@property (nonatomic, strong) MPMoviePlayerController *mpC;</span><br><span class="line"></span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line"></span><br><span class="line">- (void)viewDidLoad &#123;</span><br><span class="line">    [super viewDidLoad];</span><br><span class="line">    </span><br><span class="line">    //检测视频播放完毕 --&gt; 可以连续播放视频</span><br><span class="line">    </span><br><span class="line">    //注册通知监测视频播放完毕</span><br><span class="line"></span><br><span class="line">    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 通知绑定的方法</span><br><span class="line">- (void)moviePlayerPlaybackDidFinishNotification:(NSNotification *)notification</span><br><span class="line">&#123;</span><br><span class="line">    /**</span><br><span class="line">     MPMovieFinishReasonPlaybackEnded,  播放结束</span><br><span class="line">     MPMovieFinishReasonPlaybackError,  播放错误</span><br><span class="line">     MPMovieFinishReasonUserExited      退出播放</span><br><span class="line">     */</span><br><span class="line">    </span><br><span class="line">    //1. 获取通知结束的状态</span><br><span class="line">    NSInteger movieFinishKey = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];</span><br><span class="line">    </span><br><span class="line">    //2. 根据状态不同来自行填写逻辑代码</span><br><span class="line">    switch (movieFinishKey) &#123;</span><br><span class="line">        case MPMovieFinishReasonPlaybackEnded:</span><br><span class="line">            NSLog(@&quot;播放结束&quot;);</span><br><span class="line">            </span><br><span class="line">            // 进行视频切换 需要两步</span><br><span class="line">            </span><br><span class="line">            //1. 要想换视频, 就需要更换地址</span><br><span class="line">            self.mpC.contentURL = [[NSBundle mainBundle] URLForResource:@&quot;Alizee_La_Isla_Bonita.mp4&quot; withExtension:nil];</span><br><span class="line">            </span><br><span class="line">            // </span><br><span class="line">            [self.mpC play];</span><br><span class="line">            </span><br><span class="line">            break;</span><br><span class="line">            </span><br><span class="line">        case MPMovieFinishReasonPlaybackError:</span><br><span class="line">            NSLog(@&quot;播放错误&quot;);</span><br><span class="line">            break;</span><br><span class="line">            </span><br><span class="line">        case MPMovieFinishReasonUserExited:</span><br><span class="line">            NSLog(@&quot;退出播放&quot;);</span><br><span class="line">            </span><br><span class="line">            // 如果是不带view的播放器, 那么播放完毕(退出/错误/结束)都应该退出</span><br><span class="line">            [self.mpC.view removeFromSuperview];</span><br><span class="line">            break;</span><br><span class="line">            </span><br><span class="line">        default:</span><br><span class="line">            break;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)dealloc</span><br><span class="line">&#123;</span><br><span class="line">    //移除通知</span><br><span class="line">    [[NSNotificationCenter defaultCenter] removeObserver:self];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (IBAction)moviePlayerController</span><br><span class="line">&#123;</span><br><span class="line">    // 不带View的播放器的控制器 --&gt; 需要强引用, 设置frame, 添加到view上, 开始播放</span><br><span class="line">    //1. 获取URL地址</span><br><span class="line">    NSURL *url = [[NSBundle mainBundle] URLForResource:@&quot;Cupid_高清.mp4&quot; withExtension:nil];</span><br><span class="line">    </span><br><span class="line">    //2. 创建不带View的播放器</span><br><span class="line">    self.mpC = [[MPMoviePlayerController alloc] initWithContentURL:url];</span><br><span class="line">    </span><br><span class="line">    //3. 设置view.frame</span><br><span class="line">    self.mpC.view.frame = CGRectMake(0, 0, 300, 400);</span><br><span class="line">    </span><br><span class="line">    //4. 添加到view上</span><br><span class="line">    [self.view addSubview:self.mpC.view];</span><br><span class="line">    </span><br><span class="line">    //5. 准备播放 --&gt; 规范写法, 要写上. 调用play方法时, 会自动调用此方法</span><br><span class="line">    [self.mpC prepareToPlay];</span><br><span class="line">    </span><br><span class="line">    //6. 开始播放</span><br><span class="line">    [self.mpC play];</span><br><span class="line">    </span><br><span class="line">    //7. 控制模式</span><br><span class="line">    self.mpC.controlStyle = MPMovieControlStyleFullscreen;</span><br><span class="line">    </span><br><span class="line">    /**</span><br><span class="line">     MPMovieControlStyleNone,       // No controls</span><br><span class="line">     MPMovieControlStyleEmbedded,   // 嵌入式的控制 -- 默认</span><br><span class="line">     MPMovieControlStyleFullscreen, // 全屏时的控制样式</span><br><span class="line">     */</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>####3. iOS9 播放视频<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;AVKit/AVKit.h&gt;</span><br><span class="line">#import &lt;AVFoundation/AVFoundation.h&gt;</span><br><span class="line">@implementation ViewController</span><br><span class="line">- (IBAction)playerViewController &#123;</span><br><span class="line">    //1. 获取URL地址</span><br><span class="line">    NSURL *url = [[NSBundle mainBundle] URLForResource:@&quot;Cupid_高清.mp4&quot; withExtension:nil];</span><br><span class="line">    </span><br><span class="line">    //2. AV播放视图控制器</span><br><span class="line">    AVPlayerViewController *pVC = [AVPlayerViewController new];</span><br><span class="line">    </span><br><span class="line">    //3. 创建player --&gt; 设置时需要传入网址</span><br><span class="line">    pVC.player = [AVPlayer playerWithURL:url];</span><br><span class="line">    </span><br><span class="line">    //4. 开始播放</span><br><span class="line">    [pVC.player play];</span><br><span class="line">    </span><br><span class="line">    //5. 模态弹出</span><br><span class="line">    //[self presentViewController:pVC animated:YES completion:nil];</span><br><span class="line">    </span><br><span class="line">    //5. 如果想要自定义播放器的大小,应该自定义 --&gt; 设置frame / 添加到视图中</span><br><span class="line">    pVC.view.frame = CGRectMake(40, 200, 300, 400);</span><br><span class="line">    [self.view addSubview:pVC.view];</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>####4. 视频截图<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;AVFoundation/AVFoundation.h&gt;</span><br><span class="line"></span><br><span class="line">@interface ViewController ()</span><br><span class="line"></span><br><span class="line">@property (weak, nonatomic) IBOutlet UIImageView *imageView;</span><br><span class="line"></span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#pragma mark 点击屏幕, 开始截图</span><br><span class="line">- (void)touchesBegan:(NSSet&lt;UITouch *&gt; *)touches withEvent:(UIEvent *)event</span><br><span class="line">&#123;</span><br><span class="line">    //1. URL</span><br><span class="line">    NSURL *url = [[NSBundle mainBundle] URLForResource:@&quot;Cupid_高清.mp4&quot; withExtension:nil];</span><br><span class="line">    </span><br><span class="line">    //2. 获取资源</span><br><span class="line">    AVAsset *asset = [AVAsset assetWithURL:url];</span><br><span class="line">    </span><br><span class="line">    //3. 创建 资源图像生成器</span><br><span class="line">    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];</span><br><span class="line">    </span><br><span class="line">    //4. 开始生成图像</span><br><span class="line">    </span><br><span class="line">    //Times : 用来表示影片的时间的值</span><br><span class="line">    </span><br><span class="line">    //value: 帧数</span><br><span class="line">    //timescale: 当前视频的每秒的帧数</span><br><span class="line">    CMTime time = CMTimeMake(60, 1);</span><br><span class="line">    </span><br><span class="line">    NSValue *value = [NSValue valueWithCMTime:time];</span><br><span class="line">    </span><br><span class="line">    [imageGenerator generateCGImagesAsynchronouslyForTimes:@[value] completionHandler:^(CMTime requestedTime, CGImageRef  _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) &#123;</span><br><span class="line">        </span><br><span class="line">        //5. 主线程中更新UI</span><br><span class="line">        dispatch_sync(dispatch_get_main_queue(), ^&#123;</span><br><span class="line">            self.imageView.image = [UIImage imageWithCGImage:image];</span><br><span class="line">        &#125;);</span><br><span class="line">        </span><br><span class="line">    &#125;];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>####6. 视频录制<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;MobileCoreServices/MobileCoreServices.h&gt;</span><br><span class="line">#import &lt;MediaPlayer/MediaPlayer.h&gt;</span><br><span class="line">#import &lt;AssetsLibrary/AssetsLibrary.h&gt;</span><br><span class="line"></span><br><span class="line">@interface ViewController ()&lt;UINavigationControllerDelegate, UIImagePickerControllerDelegate&gt;</span><br><span class="line"></span><br><span class="line">@property (nonatomic, strong) MPMoviePlayerController *mpC;</span><br><span class="line"></span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line"></span><br><span class="line">- (IBAction)movieClick:(id)sender &#123;</span><br><span class="line">    //1. 判断是否可用</span><br><span class="line">    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) &#123;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    //2. 创建图像选择器</span><br><span class="line">    UIImagePickerController *picker = [UIImagePickerController new];</span><br><span class="line">    </span><br><span class="line">    //3. 设置类型</span><br><span class="line">    picker.sourceType = UIImagePickerControllerSourceTypeCamera;</span><br><span class="line">    </span><br><span class="line">    //4. 设置媒体类型</span><br><span class="line">    picker.mediaTypes = @[(NSString *)kUTTypeMovie];</span><br><span class="line">    </span><br><span class="line">    //5. 设置相机检测模式</span><br><span class="line">    picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;</span><br><span class="line">    </span><br><span class="line">    //6. 设置视频的质量</span><br><span class="line">    picker.videoQuality = UIImagePickerControllerQualityTypeHigh;</span><br><span class="line">    </span><br><span class="line">    //7. 设置代理</span><br><span class="line">    picker.delegate = self;</span><br><span class="line">    </span><br><span class="line">    //8. 模态弹出</span><br><span class="line">    [self presentViewController:picker animated:YES completion:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//UIImagePickerController 代理方法</span><br><span class="line">- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary&lt;NSString *,id&gt; *)info</span><br><span class="line">&#123;</span><br><span class="line">    NSLog(@&quot;info: %@&quot;,info);</span><br><span class="line">    </span><br><span class="line">    //1. 获取媒体类型</span><br><span class="line">    NSString *mediaTyep = info[UIImagePickerControllerMediaType];</span><br><span class="line">    </span><br><span class="line">    //2. 判断是否是视频的媒体类型</span><br><span class="line">    </span><br><span class="line">    id url = info[UIImagePickerControllerMediaURL];</span><br><span class="line">    </span><br><span class="line">    if ([mediaTyep isEqualToString:(NSString *)kUTTypeMovie]) &#123;</span><br><span class="line">        if (self.mpC == nil) &#123;</span><br><span class="line">            self.mpC = [MPMoviePlayerController new];</span><br><span class="line">            self.mpC.view.frame = self.view.bounds;</span><br><span class="line">            [self.view addSubview:self.mpC.view];</span><br><span class="line">        &#125;</span><br><span class="line">        self.mpC.contentURL = url;</span><br><span class="line">        [self.mpC play];</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    //3. 保存视频</span><br><span class="line">    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) &#123;</span><br><span class="line">        </span><br><span class="line">        //3.1 创建ALAssetsLibrary对象</span><br><span class="line">        ALAssetsLibrary *assetsLibrary = [ALAssetsLibrary new];</span><br><span class="line">        </span><br><span class="line">        //3.2 调用writeVideoAtPathToSavedPhotosAlbum即可</span><br><span class="line">        //前面的URL, 需要传入要保存的视频的URL.</span><br><span class="line">        [assetsLibrary writeVideoAtPathToSavedPhotosAlbum:url completionBlock:nil];</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    [picker dismissViewControllerAnimated:YES completion:nil];</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>####7. 视频压缩<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;MobileCoreServices/MobileCoreServices.h&gt;</span><br><span class="line">#import &lt;AVFoundation/AVFoundation.h&gt;</span><br><span class="line"></span><br><span class="line">@interface ViewController ()&lt;UINavigationControllerDelegate, UIImagePickerControllerDelegate&gt;</span><br><span class="line"></span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">- (void)touchesBegan:(NSSet&lt;UITouch *&gt; *)touches withEvent:(UIEvent *)event</span><br><span class="line">&#123;</span><br><span class="line">    //1. 判断是否可用</span><br><span class="line">    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) &#123;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    //2. 创建图像选择器</span><br><span class="line">    UIImagePickerController *picker = [UIImagePickerController new];</span><br><span class="line">    </span><br><span class="line">    //3. 设置类型</span><br><span class="line">    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;</span><br><span class="line">    </span><br><span class="line">    //4. 设置媒体类型</span><br><span class="line">    picker.mediaTypes = @[(NSString *)kUTTypeMovie];</span><br><span class="line">    </span><br><span class="line">    //5. 设置代理</span><br><span class="line">    picker.delegate = self;</span><br><span class="line">    </span><br><span class="line">    //6. 模态弹出</span><br><span class="line">    [self presentViewController:picker animated:YES completion:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#pragma mark 选中视频的时候, 进行压缩处理</span><br><span class="line">- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary&lt;NSString *,id&gt; *)info</span><br><span class="line">&#123;</span><br><span class="line">    //1. 获取媒体类型</span><br><span class="line">    NSString *mediaTyep = info[UIImagePickerControllerMediaType];</span><br><span class="line">    </span><br><span class="line">    //2. 获取视频的地址</span><br><span class="line">    id url = info[UIImagePickerControllerMediaURL];</span><br><span class="line">    </span><br><span class="line">    //3. 开始导出--&gt; 压缩</span><br><span class="line">    [self exportWithURL:url];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)exportWithURL:(NSURL *)url</span><br><span class="line">&#123;</span><br><span class="line">    //1. 获取资源</span><br><span class="line">    AVAsset *asset = [AVAsset assetWithURL:url];</span><br><span class="line">    </span><br><span class="line">    //2. 根据资源, 创建资源导出会话对象</span><br><span class="line">    //presetName : 压缩的大小</span><br><span class="line">    AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];</span><br><span class="line">   </span><br><span class="line">    //3. 设置导出路径</span><br><span class="line">    session.outputURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@&quot;12345.mov&quot;]];</span><br><span class="line">    </span><br><span class="line">    //4. 设置导出类型</span><br><span class="line">    session.outputFileType = AVFileTypeQuickTimeMovie;</span><br><span class="line">    </span><br><span class="line">    //5. 开始导出</span><br><span class="line">    [session exportAsynchronouslyWithCompletionHandler:^&#123;</span><br><span class="line">        NSLog(@&quot;当你看到这句话的时候, 恭喜你已经导出成功&quot;);</span><br><span class="line">    &#125;];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>####</p>
<p>####</p>
<p>####</p>
<p>####</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/iOS-Special-Function/iOS-sqlite3(数据库)/" rel="next" title="iOS-sqlite3(数据库)">
                <i class="fa fa-chevron-left"></i> iOS-sqlite3(数据库)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/iOS-Tool/iOS-Charles(网络监视工具)/" rel="prev" title="iOS-Charles(网络监视工具)">
                iOS-Charles(网络监视工具) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
